package cn.zebra.dev.admin.module.member.controller;

import cn.zebra.dev.admin.module.member.dto.MemberListDTO;
import cn.zebra.dev.admin.module.member.vo.MemberListQueryVO;
import cn.zebra.dev.common.constant.ParamsConstant;
import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.common.utils.ObjectUtils;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.member.module.member.entity.MemberEntity;
import cn.zebra.dev.member.module.member.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author runnable@sina.cn
 */
@Api(tags = "用户")
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @GetMapping("/curDateCountMember")
    @ApiOperation("获取今日注册会员")
    public Result curDateCountMember() {
        return Result.success(memberService.findCurDateCountMember());
    }

    @GetMapping("/list")
    @ApiOperation("用户会员列表")
    public Result<PageUtils<MemberListDTO>> list(
            MemberListQueryVO vo,
            @RequestParam(value = ParamsConstant.LIMIT, defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = ParamsConstant.PAGE, defaultValue = "1", required = false) Integer page) {
        Map<String, Object> params = ObjectUtils.objectConvertMap(vo);
        params.put("notShowPassword", true);
        params.put("showCreateTime", true);
        params.put(ParamsConstant.PAGE, page);
        params.put(ParamsConstant.LIMIT, limit);
        PageUtils<MemberEntity> pageUtils = memberService.queryPage(params);
        PageUtils<MemberListDTO> data = new PageUtils<>(pageUtils);
        List<MemberEntity> list = pageUtils.getList();
        if (list.size() > 0) {
            List<MemberListDTO> dtoList = new LinkedList<>();
            list.forEach(entity -> {
                MemberListDTO dto = new MemberListDTO();
                BeanUtils.copyProperties(entity, dto);
                dtoList.add(dto);
            });
            data.setList(dtoList);
        }
        return Result.success(data);
    }

}
